The next topic is the
principle of least privilege. There are three types of
roles in IAM: basic roles, which include the owner, editor and viewer roles that existed prior to the
introduction of IAM, predefined roles, which provide granular access for
a specific service and are managed by Google Cloud, and custom roles, which provide granular access according to a user-specified
lists of permissions. The basic roles include thousands of permissions across all Google Cloud Services. In production environments, do not grant basic roles unless
there is no alternative. Instead, grant the most
limited predefined roles or custom roles that
meets your needs. Every unnecessary
permission granted to a resource increases the
risk of exploitation. If you deploy a
Cloud Run service and you do not specify a
service account for it to use, it will use the default
service account. While that might seem harmless, it's actually a security
risk, and here's why. The default service
accounts come with a default policy binding with the editor role
on the project. The editor role is one of those extremely
powerful basic roles. Because of resource inheritance, the default service account has the permissions to create, change, and delete almost every
resource in your project. In order to avoid this pitfall, you'll need to first, create a new service account
for a Cloud Run service. Next, configured this
new service account as the Cloud Run
service Identity. Finally, add policy bindings
with predefined roles to only the resources your
code needs to access. In the next few slides, I'll go over the various steps and show what each
step actually does. The first step is to create
a new service account for a Cloud Run service
and set it as a service identity on
that Cloud Run service. This service account does not have any
permissions by default, since you've just created it, and it doesn't have
any policy binding. If you call any Google Cloud API from code that runs inside
this Cloud Run service, that call will be
rejected by IAM. In order to give the
service account permissions on the pub/sub topic,
our running example, this is what you need to add a, policy binding with
the role pub/sub publisher to the IAM policy that it's attached to the topic.